﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace ImgClip
{
    public class ClipConfig
    {
        /// <summary>
        /// base URL prefix that will be added to every requested image URL. 
        /// </summary>
        public string BaseUrl { get; set; } = "";
         

        /// <summary>
        /// 
        /// </summary>
        public string Secret { get; set; } = "";

        /// <summary>
        /// URL signature checking is disabled by default, but it is highly recommended to enable it in a production environment. To do so, the define secret pair by setting the following environment variables:
        /// </summary>
        public bool UseSignature { get; set; }



        private string _pathPrefix;
        /// <summary>
        /// URL path prefix. 
        /// </summary>
        public string PathPrefix
        {
            get { return _pathPrefix; }
            set
            {
                if (String.IsNullOrEmpty(value))
                    _pathPrefix = "/plain/";
                else
                    _pathPrefix = $"/{value.Trim('/')}/";
            }
        }

        /// <summary>
        /// User-Agent header that will be sent with source image request. 
        /// </summary>
        public string UserAgent { get; set; } = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 Edg/90.0.818.62";

        /// <summary>
        /// when true, enables using ETag HTTP header for HTTP cache control. 
        /// </summary>
        public bool UseEtag { get; set; } = true;

        /// <summary>
        /// the maximum duration (in seconds) to wait for the next request before closing the connection. When set to 0, keep-alive is disabled. Default: 10;
        /// </summary>
        public int KeepAliveTimeout { get; set; } = 10;

        /// <summary>
        /// the maximum duration (in seconds) for downloading the source image. Default: 5;
        /// </summary>
        public int DownloadTimeout { get; set; } = 5;

        /// <summary>
        /// duration (in seconds) sent in Expires and Cache-Control: max-age HTTP headers. Default: 3600 (1 hour);
        /// </summary>
        public int MaxAge { get; set; } = 3600;

        ///// <summary>
        /////  Base64-encoded image data.Supported image formats include JPG, JPEG, and PNG  
        ///// </summary>
        //public string WatermarkData { get; set; }

        private string _wmUrl;
        /// <summary>
        /// watermark image URL;
        /// </summary>
        public string WatermarkUrl
        {
            get
            {
                return _wmUrl;
            }
            set
            {
                if (!String.IsNullOrEmpty(value))
                {
                    _wmUrl = Convert.ToBase64String(Encoding.UTF8.GetBytes(WebUtility.UrlEncode(value)));
                }
            }
        }

        /// <summary>
        /// watermark base opacity;
        /// </summary>
        public double WatermarkOpacity { get; set; }


        ///// <summary>
        ///// Enable caching of the corresponding results
        ///// </summary>
        //public bool ResponseCache { get; set; }

        /// <summary>
        /// cahce minute ,default 60min
        /// </summary>
        public int CacheTime { get; set; } = 60;

        /// <summary>
        /// Locally stored image
        /// </summary>
        public bool LocalSave { get; set; }

        /// <summary>
        /// Locally stored root dir
        /// The default root is the downloads under the current application
        /// </summary>
        public string LocalRoot { get; set; } = "";

        /// <summary>
        ///  Redefines quality of the resulting image, percentage.
        /// </summary>
        public int Quality { get; set; } = 100;



    }
}
